package com.roncoo.education.oauth.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.roncoo.education.user.feign.interfaces.IFeignUsers;
import com.roncoo.education.user.feign.interfaces.vo.UsersVO;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private IFeignUsers feignUsers;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    	UsersVO userVo = feignUsers.getById(new Long(username));
    	if (userVo == null) {
            throw new UsernameNotFoundException("User not found");
        } 
    	List<GrantedAuthority> roles = new ArrayList<GrantedAuthority>();
    	roles.add(new SimpleGrantedAuthority("ADMIN"));
        User user = new User(userVo.getId().toString(), new BCryptPasswordEncoder().encode(userVo.getId().toString()), roles);
        return user;
    }
}
